/*************************************************************************
  > File Name: clientSession.hh
  > Author: zj.tian
  > Mail: t657645537@126.com 
  > Created Time: 2015年02月02日 星期一 15时52分45秒
 ************************************************************************/

#ifndef NETWORK_CLIENT_SESSION
#define NETWORK_CLIENT_SESSION

#include "__common.h"
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/enable_shared_from_this.hpp>

using namespace std;

class clientSession
: public boost::enable_shared_from_this<clientSession>
{
	public:
		clientSession(boost::asio::io_service& ioservice,NetworkCallbackFunc theCallback)
			:mySocket(ioservice),
			myStrand(ioservice),
			myNetworkCallback(theCallback)
		{
			cleanData();
		}
		~clientSession()
		{
			mySocket.close();
		}

		void sendPluginUUID(const string& thePluginUUID);

		virtual tcp::socket& socket()
		{
			return mySocket;
		};
		virtual bool is_open()
		{
			return mySocket.is_open();
		};
		virtual void start();
		void async_write(const string& theStr);
		virtual void write(const string& theStr);
		const string& getUUID()
		{
			return myUUID;		
		};
	private:
		void cleanData()
		{
			memset((void*)myData,0,max_len);
			memset((void*)myHead,0,sizeof(dataHead));
		};
		void handle_write(const boost::system::error_code& ec,size_t len);
		void handle_read_head(int __allRecevieHeadLen,const boost::system::error_code& error,std::size_t len);
		void handle_read(int __allRecevieDataLen,const boost::system::error_code& error,std::size_t len);
		// since recevie all data 
		// process msg
		void recevied();
	private:
		boost::asio::strand myStrand;
		tcp::socket mySocket;
		char myHead[sizeof(dataHead)];
		char myData[max_len];
		NetworkCallbackFunc myNetworkCallback;
		string	myUUID;
};

#endif
